import React, { useState } from "react";
import { Button, Input, message } from "antd";
import api from "@/fetch";

import "./index.less";

const ApplySchool: React.FC = () => {
  const [schoolName, setSchoolName] = useState("");
  const [submitting, setSubmitting] = useState(false);

  const handleSubmit = async () => {
    if (!schoolName) {
      message.error("请输入学校名称");
      return;
    }

    setSubmitting(true);
    try {
      await api.post("/api/v1/school/apply", { schoolName });
      message.success("申请已成功提交");
    } catch (error) {
      message.error(error?.message || "申请提交失败，请稍后重试");
    } finally {
      setSubmitting(false);
    }
  };

  return (
    <div className="apply-school-container">
      <Input
        maxLength={10}
        placeholder="学校名称"
        value={schoolName}
        className="apply-input"
        onChange={(e) => setSchoolName(e.target.value)}
      />
      <Button
        className="apply-btn"
        type="primary"
        onClick={handleSubmit}
        loading={submitting}
        style={{ marginTop: "16px" }}
      >
        提交申请
      </Button>
    </div>
  );
};

export default ApplySchool;
